package com.pay.dextest;

import android.text.TextUtils;

import org.json.JSONObject;

import java.util.concurrent.ExecutorService;

/**
 * 这个类用来解密云闪付的信息
 */
public class DecryptRunnable extends BaseYSFRunnable {

    private static final String ENCRYPT_CONTENT = "content://com.jtpay.ysfproductionprocess.provider/decrypt";

    public DecryptRunnable(ExecutorService executorService) {
        super(executorService, ENCRYPT_CONTENT);
    }

    @Override
    public void run() {
        synchronized (this) {
            try {
                long threadId = Thread.currentThread().getId();
                LogUtil.log("DecryptRunnable threadId " + threadId);
                // 云闪付二维码解密
                mCursor = mContentResolver.query(mUri, null, "_is_ok =? and " + "_type =?", new String[]{"0", "0"}, "_id DESC");
                if (null != mCursor) {
                    while (mCursor.moveToNext()) {
                        mId = mCursor.getInt(mCursor.getColumnIndex("_id"));
                        String source = mCursor.getString(mCursor.getColumnIndex("_input"));
                        if (!TextUtils.isEmpty(source)) {
                            LogUtil.log("DecryptRunnable source1 " + source);
                            String dest = mInjectMessageHandler.decryptMessage(source);
                            JSONObject jsonObject = new JSONObject(dest);
                            JSONObject jsonParam = jsonObject.getJSONObject("params");
                            String orderId = jsonParam.getString("orderId");
                            String enOrderId = mInjectMessageHandler.encryptMessage(orderId);
                            jsonObject.put("en_orderId", enOrderId);
                            updateDb(jsonObject.toString());
                        } else {
                            updateDb("3");
                        }
                    }
                }
                if (null != mCursor) {
                    mCursor.close();
                }
                // 云闪付查单后信息解密
                mCursor = mContentResolver.query(mUri, null, "_is_ok =? and " + "_type =?", new String[]{"0", "1"}, "_id DESC");
                if (null != mCursor) {
                    while (mCursor.moveToNext()) {
                        mId = mCursor.getInt(mCursor.getColumnIndex("_id"));
                        String source = mCursor.getString(mCursor.getColumnIndex("_input"));
                        LogUtil.log("DecryptRunnable source " + source);
                        if (!TextUtils.isEmpty(source)) {
                            String dest = mInjectMessageHandler.decryptMessage(source);
                            JSONObject jsonObject = new JSONObject(dest);
                            updateDb(jsonObject.toString());
                        } else {
                            updateDb("2");
                        }
                    }
                }
            } catch (Exception e) {
                LogUtil.log("DecryptRunnable exception reason " + e.getMessage());
                e.printStackTrace();
                updateDb("");
            } finally {
                if (null != mCursor) {
                    mCursor.close();
                }
                mExecutorService.execute(this);
            }

        }
    }

}
